package com.cool.service.sysmanage.impl;

import cn.hutool.core.date.DateUtil;
import com.cool.bo.sysmanage.stuff.LoginSuccessResponseDataBo;
import com.cool.mapper.sysmanagemapper.StaffPoMapper;
import com.cool.po.sysmanagepo.PermissionPo;
import com.cool.po.sysmanagepo.RolePo;
import com.cool.po.sysmanagepo.StaffPo;
import com.cool.service.sysmanage.StaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 账户信息
 *
 * @author Jiangmanman
 * @date 2020/07/25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffPoMapper staffPoMapper;

    @Override
    public StaffPo selectByAccountMsg(String username, String password) {

        if (!legitimacyCheck(username, password)) {
            return null;
        }
        StaffPo staffPo = staffPoMapper.selectByUsernameAndPasswor(username, password);

        //合法性校验
        return staffPo;
    }

    @Override
    public PageInfo<StaffPo> accordingToEntityClass(StaffPo staffPo) {

        Integer pageNum = staffPo.getPageNum();
        Integer pageSize = staffPo.getPageSize();

        PageHelper.startPage(pageNum, pageSize);

        if (Objects.isNull(staffPo.getKeyword())) {
            List<StaffPo> list = staffPoMapper.selectAllStaffPoNull(staffPo);

            return new PageInfo<>(list);
        } else {
            List<StaffPo> list = staffPoMapper.selectAllStaffPo(staffPo);

            return new PageInfo<>(list);
        }


    }

    @Override
    public Integer accordingToPrimaryKeyAccountRemoved(Integer id) {
        return staffPoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer updateAccountInformation(Integer id, StaffPo staffPo) {
        return staffPoMapper.updateByPrimaryKeySelective(staffPo);
    }

    @Override
    public Integer addAccountInformation(StaffPo staffPo) {
        return staffPoMapper.insert(staffPo);
    }

    @Override
    public PageInfo<StaffPo> fuzzyQuery(StaffPo staffPo) {

        return staffPoMapper.fuzzyQuery(staffPo);
    }

    public boolean legitimacyCheck(String username, String password) {
        return !Objects.isNull(username) && username.length() >= 1 && !Objects.isNull(password) && password.length() >= 1;
    }

    @Override
    public Integer insert(StaffPo staffPo) {

        staffPo.setStaffCreate(DateUtil.date());
        staffPo.setStaffUpdate(DateUtil.date());

        return staffPoMapper.insertSelective(staffPo);
    }

    @Override
    public List<LoginSuccessResponseDataBo.Menus> selectMsgByStaffId(Integer staffId) {
        return staffPoMapper.selectMsgByStaffId(staffId);
    }

    @Override
    public StaffPo selectByUsernameAndPassword(String username, String password) {
        return staffPoMapper.selectByUsernameAndPassword(username,password);
    }

    @Override
    public List<RolePo> queryAllRoleByStaffId(Integer staffId) {
        return staffPoMapper.queryAllRoleByStaffId(staffId);
    }

    @Override
    public List<String> getAllPermissionByRoleId(Integer staffId) {

        List<PermissionPo> list = staffPoMapper.getAllPermissionByRoleId(staffId);

        List<String> collect = list.stream().map(PermissionPo::getPermissionUri).collect(Collectors.toList());


        return collect;
    }

    @Override
    public Integer updateStatusByPrimaryKey(Integer state, Integer id) {
        return staffPoMapper.updateStatusByPrimaryKey(state, id);
    }
}
